package cn.pxs.dao;

import cn.pxs.entity.Blogs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class BlogsDao {
    @Autowired
    MongoTemplate mt;

    public List<Blogs> findAll(){
        List<Blogs> lst = mt.findAll(Blogs.class);
        return lst;
    }

    public Blogs findById(String id){
        Blogs b = mt.findById(id, Blogs.class);
        return b;
    }

    public List<Blogs> findByUid(String uid){
        List<Blogs> bs = mt.find(new Query(Criteria.where("uid").is(uid)), Blogs.class);
        return bs;
    }

    public List<Blogs> findByTypeId(String typeid){
        List<Blogs> bs = mt.find(new Query(Criteria.where("typeid").is(typeid)), Blogs.class);
        return bs;
    }

    public List<Blogs> findByTypeIdAndUid(String typeid,String uid){
        List<Blogs> bs = mt.find(new Query(Criteria.where("typeid").is(typeid).and("uid").is(uid)), Blogs.class);
        return bs;
    }

    public Blogs delById(String id){
        Blogs b = mt.findAndRemove(new Query(Criteria.where("id").is(id)), Blogs.class);
        return b;
    }

    public void add(Blogs b){
        mt.save(b);
    }




}
